''

Imports System.Collections
Imports System.ComponentModel

Namespace SystemMonitor
	''' <summary>
	''' Summary description for DataChart.
	''' </summary>
	Public Class DataChart
		Inherits System.Windows.Forms.UserControl

		''' <summary> 
		''' Required designer variable.
		''' </summary>
		Private components As System.ComponentModel.Container = Nothing
		Private _arrayList As ArrayList

		Private _colorLine As Color
		Private _colorGrid As Color

		Private _yMaxInit As Integer
		Private _gridPixel As Integer
		Private _chartType As ChartType

		#Region "Constructor/Dispose"
		Public Sub New()
			' This call is required by the Windows.Forms Form Designer.
			InitializeComponent()

			BackColor = Color.Silver

			_colorLine = Color.DarkBlue
			_colorGrid = Color.Yellow

			_yMaxInit = 1000
			_gridPixel = 0
			_chartType = ChartType.Stick

			_arrayList = New ArrayList()
		End Sub

		''' <summary> 
		''' Clean up any resources being used.
		''' </summary>
		Protected Overrides Sub Dispose(ByVal disposing As Boolean)
			If disposing Then
				If components IsNot Nothing Then
					components.Dispose()
				End If
			End If
			MyBase.Dispose(disposing)
		End Sub
		#End Region

		Public Sub UpdateChart(ByVal d As Double)
			Dim rt As Rectangle = Me.ClientRectangle
			Dim dataCount As Integer = rt.Width\2

			If _arrayList.Count >= dataCount Then
				_arrayList.RemoveAt(0)
			End If

			_arrayList.Add(d)

			Invalidate()
		End Sub

		#Region "Component Designer generated code"
		''' <summary> 
		''' Required method for Designer support - do not modify 
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			Me.Name = "DataChart"
			Me.Size = New System.Drawing.Size(150, 16)
		End Sub
		#End Region

		#region "Properties"

		<Description("Gets or sets the current Line Color in chart"), Category("DataChart")> _
		Public Property LineColor() As Color
			Get
				Return _colorLine
			End Get
			Set(ByVal value As Color)
				_colorLine = value
			End Set
		End Property

		<Description("Gets or sets the current Grid Color in chart"), Category("DataChart")> _
		Public Property GridColor() As Color
			Get
				Return _colorGrid
			End Get
			Set(ByVal value As Color)
				_colorGrid = value
			End Set
		End Property

		<Description("Gets or sets the initial maximum Height for sticks in chart"), Category("DataChart")> _
		Public Property InitialHeight() As Integer
			Get
				Return _yMaxInit
			End Get
			Set(ByVal value As Integer)
				_yMaxInit = value
			End Set
		End Property

		<Description("Gets or sets the current chart Type for stick or Line"), Category("DataChart")> _
		Public Property ChartType() As ChartType
			Get
				Return _chartType
			End Get
			Set(ByVal value As ChartType)
				_chartType = value
			End Set
		End Property

		<Description("Enables grid drawing with spacing of the Pixel number"), Category("DataChart")> _
		Public Property GridPixels() As Integer
			Get
				Return _gridPixel
			End Get
			Set(ByVal value As Integer)
				_gridPixel = value
			End Set
		End Property

		#End Region

		#Region "Drawing"
		Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
			Dim count As Integer = _arrayList.Count
			If count=0 Then
				Return
			End If

			Dim y As Double=0, yMax As Double = InitialHeight
			For i As Integer = 0 To count - 1
				y = Convert.ToDouble(_arrayList(i))
				If y>yMax Then
					yMax = y
				End If
			Next i

			Dim rt As Rectangle = Me.ClientRectangle
			y = If(yMax=0, 1, rt.Height/yMax) ' y ratio

			Dim xStart As Integer = rt.Width
			Dim yStart As Integer = rt.Height
			Dim nX, nY As Integer

			Dim pen As Pen = Nothing
			e.Graphics.Clear(BackColor)

			If GridPixels<>0 Then
				pen = New Pen(GridColor, 1)
				nX = rt.Width\GridPixels
				nY = rt.Height\GridPixels

				Dim i As Integer=1
				Do While i<=nX
					e.Graphics.DrawLine(pen, i*GridPixels, 0, i*GridPixels, yStart)
					i += 1
				Loop

				i = 1
				Do While i<nY
					e.Graphics.DrawLine(pen, 0, i*GridPixels, xStart, i*GridPixels)
					i += 1
				Loop
			End If

			' From the most recent data, so X <--------------|	
			' Get data from _arrayList	 a[0]..<--...a[count-1]

			If ChartType=ChartType.Stick Then
				pen = New Pen(LineColor, 2)

				For i As Integer = count-1 To 0 Step -1
					nX = xStart - 2*(count-i)
					If nX<=0 Then
						Exit For
					End If

					nY = CInt(Fix(yStart-y*Convert.ToDouble(_arrayList(i))))
					e.Graphics.DrawLine(pen, nX, yStart, nX, nY)
				Next i
			Else
			If ChartType=ChartType.Line Then
				pen = New Pen(LineColor, 1)

				Dim nX0 As Integer = xStart - 2
				Dim nY0 As Integer = CInt(Fix(yStart-y*Convert.ToDouble(_arrayList(count-1))))
				For i As Integer = count-2 To 0 Step -1
					nX = xStart - 2*(count-i)
					If nX<=0 Then
						Exit For
					End If

					nY = CInt(Fix(yStart-y*Convert.ToDouble(_arrayList(i))))
					e.Graphics.DrawLine(pen, nX0, nY0, nX, nY)

					nX0 = nX
					nY0 = nY
				Next i
			End If
			End If

			MyBase.OnPaint(e)
		End Sub
		#End Region
	End Class

	Public Enum ChartType
		Stick
		Line
	End Enum

End Namespace
